package studyguide.ch07;

import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
 * Displays three screens: the first with a multiple list, the second with an 
 * exclusive list, and the third with an implicit list. Clicking on next redirects 
 * to the next screen
 * @author Eduardo Ferreira
 */
public class ListMidlet extends MIDlet {

	private final List multipleList;
	private final List exclusiveList;
	private final List implicitList;
	
	private final Command nextCommand;
	private final Command exitCommand;
	
	private final Display display;
	
	private class SampleCommandListener implements CommandListener {

		public void commandAction(Command command, Displayable displayable) {
			if (command == exitCommand) {
				destroyApp(true);
				notifyDestroyed();
			} else if (command == nextCommand) {
				if (displayable == multipleList) {
					StringBuffer buffer = new StringBuffer();
					for (int i = 0; i < multipleList.size(); i++) {
						if (multipleList.isSelected(i)) {
							buffer.append("[");
							buffer.append(multipleList.getString(i));
							buffer.append("] ");
						}
					}
					System.out.println("Selected items:" + buffer.toString());
					// Going from first screen to second screen
					display.setCurrent(exclusiveList);
				} else if (displayable == exclusiveList) {
					int selectedIndex = exclusiveList.getSelectedIndex();
					String selectedLabel = exclusiveList.getString(selectedIndex);
					System.out.println("Selected item:" + selectedLabel);
					// Go to second screen
					display.setCurrent(implicitList);
				} else if (displayable == implicitList) {
					// This will never run as implicit list does not have a "next"
					throw new IllegalStateException("IMPLICIT LIST DOES NOT NEED THE NEXT BUTTON");
				} else {
					throw new RuntimeException("UNKNOWN DISPLAYABLE" + displayable);					
				}
			} else if (command == List.SELECT_COMMAND) {
				int selectedIndex = implicitList.getSelectedIndex();
				String selectedLabel = implicitList.getString(selectedIndex);
				System.out.println("Selected item:" + selectedLabel);
				// Item was selected at "implicit list". Go back to the first screen
				display.setCurrent(multipleList);
			} else {
				throw new RuntimeException("UNKNOWN COMMAND" + command);
			}
		}
		
	}
	
	public ListMidlet() {
		multipleList = new List("Multiple List", Choice.MULTIPLE);
		exclusiveList = new List("Exclusive List", Choice.EXCLUSIVE);
		implicitList = new List("Implicit List", Choice.IMPLICIT);
		
		exitCommand = new Command("Exit", Command.EXIT, 1);
		nextCommand = new Command("Next", Command.OK, 1);
		
		multipleList.addCommand(nextCommand);
		exclusiveList.addCommand(nextCommand);
		// Implicit list dont need a "next" button
		multipleList.addCommand(exitCommand);
		exclusiveList.addCommand(exitCommand);
		implicitList.addCommand(exitCommand); // Implicit lists MAY have command other that "default select"
		
		// Add options to lists
		multipleList.append("First multiple item", null);
		multipleList.append("Second multiple item", null);
		multipleList.append("Third multiple item", null);
		exclusiveList.append("First exclusive", null);
		exclusiveList.append("Second exclusive", null);
		implicitList.append("First implicit", null);
		implicitList.append("Second implicit", null);
		implicitList.append("Third implicit", null);
		
		SampleCommandListener listener = new SampleCommandListener();
		multipleList.setCommandListener(listener);
		exclusiveList.setCommandListener(listener);
		implicitList.setCommandListener(listener);
		
		display = Display.getDisplay(this);
	}
	
	protected void destroyApp(boolean unconditional) {
		// No Op
	}

	protected void pauseApp() {
		// No Op
	}

	protected void startApp() throws MIDletStateChangeException {
		System.out.println("Starting (or restarting) application");
		// Always starts on first screen
		display.setCurrent(multipleList);
	}

}
